package com.milk.milkmedia.utils;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.milk.milkmedia.R;
import com.milk.milkmedia.ui.HomePageActivity;

import java.util.List;
import java.util.Map;

/**
 * Created by 微安 on 2015/8/23.
 */
public class LyricAdapter extends BaseAdapter{

    private List<Map<String,Object>> mLyrics;
    private Context mContext;
    private LayoutInflater listContainer;           //视图容器
    private int mCurrentTime;

    class ListItemView{
        public TextView mTextView;
    }
    public LyricAdapter(Context context,List<Map<String, Object>>lyrics) {
        this.mContext = context;
        this.mLyrics = lyrics;
        listContainer = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mLyrics.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ListItemView listItemView = null;
        Resources resources = mContext.getResources();
        if (convertView == null){
            listItemView = new ListItemView();
            convertView = listContainer.inflate(R.layout.item_lyric,null);
            listItemView.mTextView = (TextView) convertView.findViewById(R.id.mplay_item_word);
            convertView.setTag(listItemView);
        }else{
            listItemView = (ListItemView) convertView.getTag();
        }
        if ((position + 1) <=mLyrics.size()- 1) {
            if ((int) mLyrics.get(position + 1).get("time") >= mCurrentTime &&
                    (int)mLyrics.get(position).get("time") <= mCurrentTime) {
                listItemView.mTextView.setTextColor(resources.getColor(R.color.black));
            } else {
                listItemView.mTextView.setTextColor(resources.getColor(R.color.tab_textgrey));
            }
        }
        listItemView.mTextView.setText(mLyrics.get(position).get("word").toString());
        Log.e(HomePageActivity.Tag, "mLyric:" + mLyrics.get(position).get("word").toString());
        return convertView;
    }


    public void sentCurrentTimePlayer(int time) {
        this.mCurrentTime  = time;
    }
}
